package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.STFilterEntity;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class CommercialConcreteFliterPopWindow extends BasePopWindow {
    private LinearLayout rootLayout;
    private TextView areaView;
    private TextView toConfigureView;
    private TextView serviceView;
    private TextView gradeView;
    private TextView okView;
    private TextView resetView;

    private MaxRecyclerView areaRecyclerView;
    private MaxRecyclerView toConfigureRecyclerView;
    private MaxRecyclerView gradeRecyclerView;
    private MaxRecyclerView serviceRecyclerView;

    private MaterialFilterAdapter3 areaAdapter;
    private MaterialFilterAdapter3 gradeAdapter;
    private MaterialFilterAdapter3 toConfigureAdapter;
    private MaterialFilterAdapter3 serviceAdapter;
    private FilterResultClick filterResultClick;
    private MaterialFilterJson materialFilterJson;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;

    public interface FilterResultClick {
        void result(MaterialFilterJson materialFilterJson);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public CommercialConcreteFliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_st_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        areaView = holder.getPop().findViewById(R.id.area_view);
        toConfigureView = holder.getPop().findViewById(R.id.to_configure_view);
        serviceView = holder.getPop().findViewById(R.id.service_view);
        gradeView = holder.getPop().findViewById(R.id.grade_view);
        areaRecyclerView = holder.getPop().findViewById(R.id.area_list);
        gradeRecyclerView = holder.getPop().findViewById(R.id.grade_list);
        toConfigureRecyclerView = holder.getPop().findViewById(R.id.to_configure_list);
        serviceRecyclerView = holder.getPop().findViewById(R.id.service_list);

        areaRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        gradeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        toConfigureRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        serviceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));

        //储仓
        areaAdapter = new MaterialFilterAdapter3(mContext, null);
        areaAdapter.setDefItem(-1);
        areaAdapter.setMultiSelect(true);
        areaAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<DictEntity3>() {
            @Override
            public void onItemMultiClick(List<DictEntity3> dictEntity3s, int position) {
                areaList.clear();
                for (DictEntity3 filterEntity : dictEntity3s) {
                    areaList.add(filterEntity.getId());
                }
//                materialFilterJson.setAreaId(areaList);
            }
        });
        areaRecyclerView.setAdapter(areaAdapter);

        //强度等级
        gradeAdapter = new MaterialFilterAdapter3(mContext, null);
        gradeAdapter.setItemSelect(true);
        gradeAdapter.setDefItem(-1);
        gradeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setStrengthId(materialFilterEntity.getId());
            }
        });
        gradeRecyclerView.setAdapter(gradeAdapter);

        //配置
        toConfigureAdapter = new MaterialFilterAdapter3(mContext, null);
        toConfigureAdapter.setItemSelect(true);
        toConfigureAdapter.setDefItem(-1);
        toConfigureAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setGradationId(materialFilterEntity.getId());

            }
        });
        toConfigureRecyclerView.setAdapter(toConfigureAdapter);

        //服务
        serviceAdapter = new MaterialFilterAdapter3(mContext, null);
        serviceAdapter.setItemSelect(true);
        serviceAdapter.setDefItem(-1);
        serviceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setServiceId(materialFilterEntity.getId());
            }
        });
        serviceRecyclerView.setAdapter(serviceAdapter);

        materialFilterJson = new MaterialFilterJson();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
              /*  tempMaterKeyList.clear();
                tempMaterValueList.clear();
                for (int i = 0 ; i < materValueList.size(); i ++) {
                    if (!materValueList.get(i).equals("")) {
                       tempMaterValueList.add(materValueList.get(i));
                       tempMaterKeyList.add(materKeyList.get(i));
                    }
                }
                materialFilterJson.setMaterKey(tempMaterKeyList);
                materialFilterJson.setMaterValue(tempMaterValueList);*/
                if (filterResultClick != null) {
                    filterResultClick.result(materialFilterJson);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        materialFilterJson.setMaterKey(null);
        materialFilterJson.setMaterValue(null);
        materialFilterJson.setBrandId(new ArrayList<>());

        //重置服务
        serviceAdapter.setDefItem(-1);
        materialFilterJson.setServiceId(null);

        //重置配置
        toConfigureAdapter.setDefItem(-1);
        materialFilterJson.setGradationId(null);

        //重置强度等级
        gradeAdapter.setDefItem(-1);
        materialFilterJson.setStrengthId(null);

        //重置储仓库
        areaList.clear();
        materialFilterJson.setAreaId(null);
        areaAdapter.resetCheckDatas();
    }

    public void setData(STFilterEntity data){
        //储仓
        setAreaData(data.getArea());
        //强度等级
        setGradeData(data.getStrength());
        //配置
        setToConfigureData(data.getGradation());
        //服务
        setServiceData(data.getServiceType());
    }

    public void setAreaData(List<DictEntity3> data) {
        if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
        }
        areaAdapter.setDatas(data);
    }

    public void setGradeData(List<DictEntity3> data) {
        if (data.size() == 0) {
            gradeView.setVisibility(View.GONE);
        } else {
            gradeView.setVisibility(View.VISIBLE);
        }
        gradeAdapter.setDatas(data);
    }

    public void setToConfigureData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            toConfigureView.setVisibility(View.GONE);
        } else {
            toConfigureView.setVisibility(View.VISIBLE);
        }
        toConfigureAdapter.setDefItem(-1);
        toConfigureAdapter.setDatas(data);
    }

    public void setServiceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            serviceView.setVisibility(View.GONE);
        } else {
            serviceView.setVisibility(View.VISIBLE);
        }
        serviceAdapter.setDatas(data);
    }

    public void setSpecData(List<DictEntity3> data) {
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getName());
            materValueList.add("");
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(16);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_333333));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter childAdapter = new MaterialFilterAdapter(mContext, null);
        childAdapter.setItemSelect(true);
        childAdapter.setDefItem(-1);
        childAdapter.setIndex(index);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        //    childAdapter.setDatas(data);
//        childAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
//            @Override
//            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
//                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getId());
//            }
//        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
